package cn.fl.custpay.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.custpay.api.ICmmOlAutoCharRecApiService;
import cn.fl.custpay.constant.CustPayConstant;
import cn.fl.custpay.dto.CmmOlAutoCharRecDTO;
import cn.fl.custpay.service.ICmmOlAutoCharRecService;
import cn.fl.custpay.transform.CmmOlAutoCharRecVOTransform;
import cn.fl.custpay.vo.CmmOlAutoCharRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUsrVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * @Description: 在线自动代扣设置记录 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CmmOlAutoCharRecServiceImpl implements ICmmOlAutoCharRecService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CmmOlAutoCharRecServiceImpl.class);
	
	/**
	 * 在线自动代扣设置记录API
	 */
	@Autowired
	private ICmmOlAutoCharRecApiService cmmOlAutoCharRecApiService;

    /**
     * 数据字典服务
     */
	@Autowired
    private ISysDictionaryService sysDictionaryService;

	/**
	 * 用户服务
	 */
	@Autowired
	private ISysUsrService sysUsrService;


	@Override
	public CmmOlAutoCharRecVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		CmmOlAutoCharRecDTO dto = null;//this.自定义ApiService.selectCmmOlAutoCharRecDTOById(id);
		return CmmOlAutoCharRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(CmmOlAutoCharRecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		//return this.cmmOlAutoCharRecApiService.add(CmmOlAutoCharRecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除对象主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		//return this.cmmOlAutoCharRecApiService.removeCmmOlAutoCharRecDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(CmmOlAutoCharRecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		//return this.cmmOlAutoCharRecApiService.modifyCmmOlAutoCharRecDTO(CmmOlAutoCharRecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        if(null == page){
            page = new Pager();
        }
        Pager pager = this.cmmOlAutoCharRecApiService.searchListPage(page);
        List<CmmOlAutoCharRecDTO> list = (List<CmmOlAutoCharRecDTO>) pager.getResultList();
        /***
         *	数据字典CD名称转换
         **/
        //数据字典
        List<String> codeGrpCdList = new ArrayList<>();
        //终端平台
        codeGrpCdList.add(DictionaryConstant.CODE_OP_TYP_CD);
        //自动代扣操作结果：0关闭，1开启
        codeGrpCdList.add(CustPayConstant.AUTO_DK_RESULT_CD);

        Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
        List<KeyValueVO> sysList = dicMap.get(DictionaryConstant.CODE_OP_TYP_CD);
        List<KeyValueVO> autoDkReusltList = dicMap.get(CustPayConstant.AUTO_DK_RESULT_CD);
        Map<String, String> sysMap = null;
        if (CollectionUtils.isNotEmpty(sysList)) {
            sysMap = new HashMap<>(sysList.size());
            for (KeyValueVO dto : sysList) {
                sysMap.put(dto.getKey(), dto.getValue());
            }
        }
        Map<String, String> autoDkReusltMap = null;
        if (CollectionUtils.isNotEmpty(autoDkReusltList)) {
            autoDkReusltMap = new HashMap<>(autoDkReusltList.size());
            for (KeyValueVO dto : autoDkReusltList) {
                autoDkReusltMap.put(dto.getKey(), dto.getValue());
            }
        }
        List<CmmOlAutoCharRecVO> CmmOlAutoCharRecVOList = CmmOlAutoCharRecVOTransform.toVOList(list);
        for (CmmOlAutoCharRecVO cmmOlAutoCharRecVO : CmmOlAutoCharRecVOList) {
            if (sysMap != null && sysMap.containsKey(cmmOlAutoCharRecVO.getPlatformCd())) {
                cmmOlAutoCharRecVO.setPlatformCdNm(sysMap.get(cmmOlAutoCharRecVO.getPlatformCd()));
            }
            String isAutoStr = String.valueOf(cmmOlAutoCharRecVO.getIsAuto());
            if (autoDkReusltMap != null && autoDkReusltMap.containsKey(isAutoStr)) {
                cmmOlAutoCharRecVO.setIsAutoNm(autoDkReusltMap.get(isAutoStr));
            }
            if(StringUtils.isNotEmpty(cmmOlAutoCharRecVO.getCrtUsrId())){
				try{
					SysUsrVO sysUsrVO = this.sysUsrService.selectById(cmmOlAutoCharRecVO.getCrtUsrId(),CurrentThreadContext.getCurrentSysCd());
					if(sysUsrVO != null){
						cmmOlAutoCharRecVO.setCrtNm(sysUsrVO.getRefRlNm());
					}
				}catch(Exception e){
					BaseLogUtils.info(logger, "searchListPage", "查询用户异常!" + e.getMessage(),e);
				}
			}
        }
        pager.setResultList(CmmOlAutoCharRecVOList);
        return pager;
	}
}
